package com.xiaoha.pogy.file;

import com.xiaoha.pogy.dao.ContactRepository;
import com.xiaoha.pogy.entities.Book;
import com.xiaoha.pogy.entities.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("file")
public class FileIndexController {

    @Autowired
    ContactRepository contactRepository;

    @GetMapping

    public String displayContactshelves(Model model) {
        List<Contact> contacts = contactRepository.findAll();
        model.addAttribute("contacts", contacts);
        return "files/list-files";
    }

    @GetMapping("/new")
    public String file_index(Model model){
        Contact aContact = new Contact();
        model.addAttribute("contact",aContact);

        return "files/new-files";
    }
    @PostMapping("/file/save")
    public String createContact(Model model,Contact contact){

        //使用student crudRepository保存到数据库
        contactRepository.save(contact);
        return "redirect:/file";
    }
}
